package ui;

import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.geom.Rectangle2D;

import javax.swing.JPanel;
 
public class Grid extends JPanel {
    /**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	/**
	 * 
	 */
	
	public final int BACKGROUND_TYPE = 0;
	public final int ROBOT_TYPE = 1;
	public final int CANDY_TYPE = 2;
	public final int OBSTACLE_TYPE = 3;
	public final int ROBOT_WITH_CANDY_TYPE = 4;

	public SquareRx[][] squares;
    public int PAD = 10;
    public int ROWS;
    public int COLS;
    
    public int nr_of_robots;
    public int map_width;
    public int map_height;
    public int obstacle_width;
    public int obstacle_height;
    
 
 
    public Grid(int nr_of_robots, int map_width,
			int map_height, int obstacle_width, int obstacle_height) {
		this.nr_of_robots = nr_of_robots;		
		this.obstacle_width = obstacle_width;
		this.obstacle_height = obstacle_height;
		this.ROWS = map_width;
		this.COLS = map_height;
	}

	protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        Graphics2D g2 = (Graphics2D)g;
        g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
                            RenderingHints.VALUE_ANTIALIAS_ON);
        if(squares == null) {
            initSquares();
        }
        // Draw squares.        
        for(int i = 0; i < ROWS; i++) {
            for(int j = 0; j < COLS; j++) {
            	
                squares[i][j].draw(g2);
            }
        }
    }
 
    private void initSquares() {
        squares = new SquareRx[ROWS][COLS];
        int w = getWidth();
        int h = getHeight();
        double xInc = (double)(w - 2*PAD)/COLS;
        double yInc = (double)(h - 2*PAD)/ROWS;
        for(int i = 0; i < ROWS; i++) {
            double y = PAD + i*yInc;
            for(int j = 0; j < COLS; j++) {
                double x = PAD + j*xInc;
                Rectangle2D.Double r =
                    new Rectangle2D.Double(x, y, xInc, yInc);
                squares[i][j] = new SquareRx(i, j, r);
            }
        }
        
        setObstacle();
        
        setRobots();
        
        setCandy();

        
        
    }
    
    public void setSquare(int i, int j, int type) {
    	switch(type) {
    	
    	//background type
    	case 0 :squares[i][j].setBackground_status(true);
    			squares[i][j].setCandy_status(false); 
    			squares[i][j].setObstacle_status(false); 
    			squares[i][j].setRobot_holding_candy_status(false); 
    			squares[i][j].setRobot_status(false);
    			break;
    	
    	//robot type
    	case 1 :squares[i][j].setBackground_status(false);
				squares[i][j].setCandy_status(false); 
				squares[i][j].setObstacle_status(false); 
				squares[i][j].setRobot_holding_candy_status(false); 
				squares[i][j].setRobot_status(true);
				break;
		
		//candy type		
    	case 2 :squares[i][j].setBackground_status(false);
				squares[i][j].setCandy_status(true); 
				squares[i][j].setObstacle_status(false); 
				squares[i][j].setRobot_holding_candy_status(false); 
				squares[i][j].setRobot_status(false);
				break;
		
		//obstacle type		
    	case 3 :squares[i][j].setBackground_status(false);
				squares[i][j].setCandy_status(false); 
				squares[i][j].setObstacle_status(true); 
				squares[i][j].setRobot_holding_candy_status(false); 
				squares[i][j].setRobot_status(false);
				break;
		
		//robot with candy type		
    	case 4 :squares[i][j].setBackground_status(false);
				squares[i][j].setCandy_status(false); 
				squares[i][j].setObstacle_status(false); 
				squares[i][j].setRobot_holding_candy_status(true); 
				squares[i][j].setRobot_status(false);
				break;
		   
		//reset square to default		
    	default: 
	    		squares[i][j].setBackground_status(false);
				squares[i][j].setCandy_status(false); 
				squares[i][j].setObstacle_status(false); 
				squares[i][j].setRobot_holding_candy_status(false); 
				squares[i][j].setRobot_status(false);
				break;
    	}
    	
    	//repaint the world
    	repaint();
    }
    
    private void setCandy() {
    	//draw candy       
    	int position_row = ROWS/2;
        int position_col = COLS;
        for ( int q = position_row - nr_of_robots/3 ; q <= position_row ; q = q + 1 ) {        	
        	for ( int qq = position_col - 3 ; qq < position_col ; qq = qq + 1) {        		
        		setSquare(q, qq, CANDY_TYPE);   		
        	}               
        }
		
	}

	private void setRobots() {
    	//draw initial robots 
		int position_row = ROWS/2;
        int position_col = 0;
        for ( int q = position_row - nr_of_robots/3 ; q <= position_row ; q = q + 1 ) {        	
        	for ( int qq = position_col; qq <= 2 ; qq = qq + 1) {        		
        		setSquare(q, qq, ROBOT_TYPE);   		
        	}               
        }		
	}

	private void setObstacle() {
    	//obstacle in middle
        int middle_row = ROWS/2;
        int middle_col = COLS/2;       
        for ( int q = middle_row - obstacle_height/2 ; q < middle_row + obstacle_height/2 ; q = q + 1 ) {        	
        	for ( int qq = middle_col - obstacle_width/2; qq < middle_col + obstacle_width/2 ; qq = qq + 1) {        		
        		setSquare(q, qq, OBSTACLE_TYPE);    		
        	}          
        }        
    } 
	
	
    
   
}
 
